package com.slg.android.manyanime.utils;

import android.content.Context;
import android.os.Environment;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.slg.android.manyanime.bean.User;

import java.io.File;

/**
 * Created by E540 on 2015/3/11.
 */
public class DataStore {
    private static BitmapUtils bitmapUtils;
    private static DataStore dataStore;
    private User user;
    private DbUtils db;

    public static void createInstance(Context context) {
        dataStore = new DataStore(context);
        File diskCacheDir = null;
        if (context != null) {
            //上下文有，那么进行存储设置
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                diskCacheDir = context.getExternalCacheDir();
            } else {
                diskCacheDir = context.getCacheDir();
            }
        } else {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                File dir = Environment.getExternalStorageDirectory();
                diskCacheDir = new File(dir, ".ManyAnime");
            }
        }
        if (diskCacheDir != null) {
            bitmapUtils = new BitmapUtils(context, diskCacheDir.getAbsolutePath());
        } else {
            bitmapUtils = new BitmapUtils(context);
        }
    }

    public User getUser() {
        return user;
    }
    public DbUtils getDb(){
        return db;
    }
    public void setUser(User user) {
        this.user = user;
    }

    public static DataStore getDataStore() {
        if (dataStore == null) {
            throw new RuntimeException("你必须在application中初始化DataStore");
        }
        return dataStore;
    }

    public static BitmapUtils getBitmapUtils() {
        if (bitmapUtils == null) {
            throw new RuntimeException("你必须在application中初始化DataStore");
        }
        return bitmapUtils;
    }

    private DataStore(Context context) {
        db = DbUtils.create(context);
    }
}
